package ch.elca.training.dao.common;

import net.sf.cglib.proxy.Enhancer;
import ch.elca.el4j.services.persistence.generic.dao.DaoRegistry;
import ch.elca.el4j.services.persistence.generic.dao.GenericDao;
import ch.elca.training.dao.EmployeeDao;
import ch.elca.training.dao.ProjectDao;
import ch.elca.training.dom.Employee;
import ch.elca.training.dom.Project;

public class DaoProvider {
	 /**
     * Dao registry.
     */
    private static DaoRegistry daoRegistry = null;

    private EmployeeDao employeeDao;
    private ProjectDao projectDao;
    
    /**
     * @param daoRegistry the daoRegistry to set
     */
    public void setDaoRegistry(DaoRegistry daoRegistry) {
        DaoProvider.daoRegistry = daoRegistry;
    }
    
    /**
     * Get the DAO corresponding to the given BaseDom. This is kind of
     * abstract level for the DAO. This kind of abstract level is extremely
     * useful in reducing if-then-else code when manipulate DOM.
     * 
     * @param daoClass clazz whose corresponding DAO need to be retrieved.
     * @return {@link GenericDao}.
     */
    @SuppressWarnings("unchecked")
    public GenericDao getGenericDao(Class daoClass) {
        Class clazz = daoClass;
        if (Enhancer.isEnhanced(daoClass)) {
            clazz = daoClass.getSuperclass(); 
        }
        
        GenericDao refDao 
            = (GenericDao) daoRegistry.getFor(clazz);
        
        return refDao;
    }
    
    public EmployeeDao getEmployeeDAO() {
    	return (EmployeeDao) getGenericDao(Employee.class);
    }
    
    public ProjectDao getProjectDAO() {
    	return (ProjectDao) getGenericDao(Project.class);
    }

	/**
	 * @param employeeDao the employeeDao to set
	 */
	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	/**
	 * @return the employeeDao
	 */
	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	/**
	 * @param projectDao the projectDao to set
	 */
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * @return the projectDao
	 */
	public ProjectDao getProjectDao() {
		return projectDao;
	}
}
